
// 1.将base64图片转换成blod格式
export function dataURLtoBlob(dataurl) {
  var arr = dataurl.split(',')
  var mime = arr[0].match(/:(.*?);/)[1]
  var bstr = atob(arr[1])
  var n = bstr.length
  var u8arr = new Uint8Array(n)
  while (n--) {
    u8arr[n] = bstr.charCodeAt(n)
  }
  return new Blob([u8arr], { type: mime })
}

//将富文本上传的文件base64对象转换为file
export function getImgFile(url) {
  return blobToFile(dataURLtoBlob(url))
}

// 2.将blod转换为file
export function blobToFile(theBlob, fileName) {
  theBlob.lastModifiedDate = new Date()
  theBlob.name = fileName
  return theBlob
}
// 3.富文本编辑器找到img标签转换为file对象返回
export function findImgtags(detail) {
  const fileArr = []
  const contentImg = []

  detail.replace(/<img [^>]*src=['"]([^'"]+)[^>]*>/g, function (match, capture) {
    contentImg.push(capture)
  })
  contentImg.forEach((v, i) => {
    // 排除网图或者未上传的
    if (v.indexOf('base64') !== -1) {
      var file = blobToFile(dataURLtoBlob(v), i)
      fileArr.push(file)
    }
  })
  return fileArr
}
// /将富文本编辑器的img的src提取
export function findImgtags1(detail) {
  const contentImg = []
  detail.replace(/<img [^>]*src=['"]([^'"]+)[^>]*>/g, function (match, capture) {
    contentImg.push(capture)
  })
  return contentImg
}
// 4.富文本编辑器找到img标签转换为file对象返回
export function findImgAll(detail) {
  const contentImg = []

  detail.replace(/<img [^>]*src=['"]([^'"]+)[^>]*>/g, function (match, capture) {
    contentImg.push(capture)
  })

  return contentImg
}
// 5.将上传后的图片替换出路径,增加，修改要先找出base64的
export function replaceTxt1(detail, replaceArrUrl) {
  var contentImg = []
  var localcontentImg = []
  detail.replace(/<img [^>]*src=['"]([^'"]+)[^>]*>/g, function (match, capture) {
    contentImg.push(capture)
  })
  contentImg.forEach(v => {
    if (v.indexOf('base64') !== -1) {
      localcontentImg.push(v)
    }
  })
  if (localcontentImg.length > 0) {
    // 开始替换图片url
    for (var i = 0; i < localcontentImg.length; i++) {
      detail = detail.replace(localcontentImg[i], replaceArrUrl[i])
    }
  }
  return detail
}
// 截取七牛返回字符串中的key
export function getqiniukey(str) {
  var start = str.indexOf('"') + 1
  var end = str.indexOf(',') - 1
  return str.substring(start, end)
}

